#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

"""

"""

from matrix_tools import *
from linear_tools import *

def greator_lessor(gcd_matrix, greator_idx):
    # print('get %s' % locals())
    greator = gcd_matrix[greator_idx][0]
    lessor = gcd_matrix[1-greator_idx][0]

    return greator, lessor

def enough_gcd(gcd_matrix, greator_idx):
    # print("enough %s" % locals())
    greator, lessor = greator_lessor(gcd_matrix, greator_idx)
    # greator = gcd_matrix[greator_idx][0]
    # lessor = gcd_matrix[1-greator_idx][0]

    if greator % lessor == 0:
        return True
    else:
        return False

def gcd_proceed(gcd_matrix, greator_idx):
    greator, lessor = greator_lessor(gcd_matrix, greator_idx)
    multiple = greator // lessor

    lessor_idx = 1 - greator_idx

    coefficients = [0, 0]
    coefficients[greator_idx] = 1
    coefficients[lessor_idx]  = -multiple
    gcd_matrix[greator_idx] = linear_combination(gcd_matrix, coefficients)

def gcd(a, b):
    gre, le = max([a,b]), min([a,b])
    gcd_numbers = [
        [le],
        [gre],
    ]

    gcd_matrix = matrix_append_col(gcd_numbers, identity(2))
    show_matrix(gcd_matrix, 'gcd')
    # print(gcd)

    greator_idx = 1
    while not enough_gcd(gcd_matrix, greator_idx):
        gcd_proceed(gcd_matrix, greator_idx)
        greator_idx = 1 - greator_idx

    lessor_idx = 1 - greator_idx
    return gcd_matrix[lessor_idx][0], gcd_matrix[lessor_idx][1:]


def test():
    cd, comb = gcd(100, 6)
    print(cd, comb)


def main():
    print("start main")
    test()


if __name__ == "__main__":
    main()
